﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer.FindArtInfo;
using BusinessLayer.FindArtInfo.Entities;
using BusinessLayer.FindArtInfo.Models;
using CommandLib;
using FindArtInfo.Common;
using FindArtInfo.Models;

namespace FindArtInfo.Controllers
{
    public class SignaturesController : MultiBase
    {
        //
        // GET: /Signatures/        
        [CheckExpriesDateAttribute]
        public ActionResult ListSignatures(int artistsId)
        {
            PageSignaturesModel model = new PageSignaturesModel();
            model.Artist = new ArtistsBL().GetById(artistsId);
            model.ListSignatures = new SignaturesBL().GetByArtistsId(artistsId);
            return View(model);
        }

        public ActionResult Letter(string letter, int? pageIndex)
        {
            int pageSize = 60;
            PageSignaturesLetterModel model = new PageSignaturesLetterModel();
            model.Letter = string.IsNullOrEmpty(letter) ? "A" : letter;
            model.ListArtists = new SignaturesBL().GetByLastNameLetter(string.IsNullOrEmpty(letter) ? "A" : letter, Convert.ToInt32(pageIndex == null ? 1 : pageIndex), pageSize);
            model.PageIndex = pageIndex == null ? 1 : Convert.ToInt32(pageIndex);
            model.PageSize = pageSize;
            if ((pageIndex == null ? 1 : pageIndex) == 1 || Session[Constants.SessionKey.SESSION_TOTAL_RECORD_SIGN_LETTER] == null)
            {
                int totalRecord;
                new SignaturesBL().GetCountByLastNameLetter(string.IsNullOrEmpty(letter) ? "A" : letter, out totalRecord);
                Session[Constants.SessionKey.SESSION_TOTAL_RECORD_SIGN_LETTER] = totalRecord;
            }
            model.TotalRecord = Convert.ToInt32(Session[Constants.SessionKey.SESSION_TOTAL_RECORD_SIGN_LETTER]);
            model.TotalPage = model.TotalRecord % model.PageSize == 0
                                  ? model.TotalRecord / model.PageSize
                                  : (model.TotalRecord / model.PageSize) + 1;
            model.Url = string.IsNullOrEmpty(letter) ? "/browse-signatures" : string.Format("/{0}/browse-signatures", letter);
            return View(model);
        }

        public ActionResult ListPictures(int? itemSelected, int? sort, int? pageIndex)
        {
            int pageSize = 8;
            PageSignaturesPicturesModel model = new PageSignaturesPicturesModel();                        
            //model.PageIndex = pageIndex == null ? 1 : Convert.ToInt32(pageIndex);
            model.PageSize = pageSize;
            model.ItemSelected = itemSelected == null ? 0 : itemSelected;
            model.Order = sort;
            
            //if ((pageIndex == null ? 1 : pageIndex) == 1 || Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PICTURES_SIGN] == null)
            //{
                int totalRecord;
                new SignaturesBL().GetCount(out totalRecord);
                //Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PICTURES_SIGN] = totalRecord;
            //}
            //model.TotalRecord = Convert.ToInt32(Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PICTURES_SIGN]);
            model.TotalRecord = totalRecord;
            model.TotalPage = model.TotalRecord % model.PageSize == 0
                                  ? model.TotalRecord / model.PageSize
                                  : (model.TotalRecord / model.PageSize) + 1;            
            model.Url = string.Format("/0/{0}/artist-signatures",
                                            sort == null ? 0 : sort);

            if (sort != null && sort == Constants.Order.ASC)
            {
                model.PageIndex = pageIndex == null ? 1 : Convert.ToInt32(pageIndex);
            }
            else
            {
                model.PageIndex = pageIndex == null ? model.TotalPage : Convert.ToInt32(pageIndex);
            }

            List<SignaturesDetailExtend> listSignatures = new SignaturesBL().GetAll(model.PageIndex, pageSize, sort);
            model.ListSignatures = listSignatures;
            if (listSignatures != null && listSignatures.Count >= model.ItemSelected)
            {
                model.SignaturesSelected = listSignatures[Convert.ToInt32(model.ItemSelected)];
            }

            return View(model);
        }
    }
}
